<!--
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2019-08-05 15:16:04
 * @LastEditTime: 2019-11-04 14:49:39
 * @LastEditors: Please set LastEditors
 -->
<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>Hello APP</title>
    <link rel="stylesheet" type="text/css" href="../css/vant.css">
    <link rel="stylesheet" type="text/css" href="../css/base.css" />
    <script type="text/javascript" src="../script/jquery.js"></script>
    <script type="text/javascript" src="../script/layer/mobile/layer.js"></script>
    <script type="text/javascript" src="../script/vue.min.js"></script>
    <script type="text/javascript" src="../script/vant.min.js"></script>
    <script type="text/javascript" src="../script/fastclick.js"></script>
    <script type="text/javascript" src="../script/base.js"></script>
    <script type="text/javascript" src="../script/db.js"></script>
    <style type="text/css">
        .row {
            margin-top: 10px;
            position: relative;
            overflow: hidden;
        }

        .row:last-child {
            margin-bottom: 10px;
        }

        .ov {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            font-size: 14px;
        }

        /* .van-cell__value {
            text-align: right;
        } */

        .label {
            font-weight: 500;
            font-size: 13px;
            color: #969799;
        }

        .seal {
            width: 60px;
            top: 0;
            right: 0;
        }

        .delete-margin {
            margin-left: 40px;
        }

        .delete {
            position: absolute;
            left: 0;
            top: 0;
            width: 50px;
            height: 68px;
            line-height: 70px;
            text-align: center;
            background: #fff;
            padding-top: 5px;
            box-shadow: 0 0 5px rgb(200, 198, 198);
        }
    </style>
</head>

<body>
    <div id="app" v-cloak>
        <div class="init-full">
            <van-nav-bar class="head" title="草稿箱" left-arrow @click-left="onClickLeft">
                <div slot="right" class="head-right">
                    <!-- <span @click="showSearch = true;">
                        <img src="../image/search.png" />
                    </span> -->
                    <span @click="showDelete = !showDelete;">{{showDelete?'完成':'编辑'}}</span>
                </div>
            </van-nav-bar>
            <div class="body-no-bottom">
                <div class="content">
                    <van-tabs v-model="tabActive">
                        <van-tab title="销售出库">
                            <div class="row" v-for="(row,index) in dataList1" @click="intoRow(row)">
                                <div class="delete" v-if="showDelete" @click.stop="deleteRow(1,row,index)">
                                    <van-icon name="delete" size="26" color="#cd1b21"></van-icon>
                                </div>
                                <van-cell :class="showDelete?'delete-margin':''">
                                    <div>
                                        <van-row gutter="20">
                                            <van-col span="14" class="ov">{{getClientName(row)}}</van-col>
                                            <van-col span="10">{{row.CreateTime}}</van-col>
                                        </van-row>
                                    </div>
                                    <div class="label">
                                        <van-row gutter="20">
                                            <van-col span="14">合计数量:{{getTotalNum(row)}}
                                            </van-col>
                                            <van-col span="10">合计金额:{{getTotalPrice1(row)}}</van-col>
                                        </van-row>
                                    </div>

                                </van-cell>
                            </div>
                        </van-tab>
                        <van-tab title="收款单">
                            <div class="row" v-for="(row,index) in dataList2" @click="intoRow(row)">
                                <div class="delete" v-if="showDelete" @click.stop="deleteRow(2,row,index)">
                                    <van-icon name="delete" size="26" color="#cd1b21"></van-icon>
                                </div>
                                <van-cell :class="showDelete?'delete-margin':''">
                                    <div>
                                        <van-row gutter="20">
                                            <van-col span="14" class="ov">{{getClientName(row)}}</van-col>
                                            <van-col span="10">{{row.CreateTime}}</van-col>
                                        </van-row>
                                    </div>
                                    <div class="label">
                                        <van-row gutter="20">
                                            <van-col span="10">合计金额:{{getTotalPrice2(row)}}</van-col>
                                        </van-row>
                                    </div>
                                </van-cell>
                            </div>
                        </van-tab>
                        <van-tab title="调货单">
                            <div class="row" v-for="(row,index) in dataList3" @click="intoRow(row)">
                                <div class="delete" v-if="showDelete" @click.stop="deleteRow(3,row,index)">
                                    <van-icon name="delete" size="26" color="#cd1b21"></van-icon>
                                </div>
                                <van-cell :class="showDelete?'delete-margin':''">
                                    <div>
                                        <van-row gutter="20">
                                            <van-col span="14" class="ov">{{getClientName(row)}}</van-col>
                                            <van-col span="10">{{row.CreateTime}}</van-col>
                                        </van-row>
                                    </div>
                                    <div class="label">
                                        <van-row gutter="20">
                                            <van-col span="14">合计进价:{{getTotalPrice2(row)}}</van-col>
                                            <van-col span="10">合计售价:{{getTotalPrice3(row)}}</van-col>
                                        </van-row>
                                    </div>
                                </van-cell>
                            </div>
                        </van-tab>
                        <van-tab title="活动充值单">
                            <div class="row" v-for="(row,index) in dataList4" @click="intoRow(row)">
                                <div class="delete" v-if="showDelete" @click.stop="deleteRow(4,row,index)">
                                    <van-icon name="delete" size="26" color="#cd1b21"></van-icon>
                                </div>
                                <van-cell :class="showDelete?'delete-margin':''">
                                    <div>
                                        <van-row gutter="20">
                                            <van-col span="14" class="ov">{{getClientName(row)}}</van-col>
                                            <van-col span="10">{{row.CreateTime}}</van-col>
                                        </van-row>
                                    </div>
                                    <div class="label">
                                        <van-row gutter="20">
                                            <van-col span="14">活动套餐:{{getActiveName(row)}}</van-col>
                                            <van-col span="10">充值金额:{{getRechargePrice(row)}}</van-col>
                                        </van-row>
                                    </div>
                                </van-cell>

                            </div>
                        </van-tab>
                    </van-tabs>
                </div>
                <van-loading v-if="showLoading" />
            </div>
        </div>

        <van-popup v-model="showSearch" round position="bottom">
            <div>
                <van-cell title="往来单位" :value="searchData.S_ClientName" is-link @click="openScreen(-1,10)">
                </van-cell>
                <van-cell title="开始时间" :value="searchData.S_SDate" @click="openDate(1)">
                </van-cell>
                <van-cell title="结束时间" :value="searchData.S_EDate" @click="openDate(2)">
                </van-cell>
            </div>
            <div class="btn-padding">
                <van-button type="primary" block style="border-radius: 0;" @click="searchList">查询</van-button>
            </div>
        </van-popup>


        <van-popup v-model="showDate" round position="bottom">
            <van-datetime-picker v-model="currentDate" type="date" @confirm="confirmDate" @cancel="cancelDate"
                cancel-button-text="清空" />
        </van-popup>
    </div>
</body>

<script type="text/javascript">
    var vm;
    apiready = function () {
        //$('.init-full').show();

        api.addEventListener({
            name: 'selectRow'
        }, function (ret, err) {
            var selectType = ret.value.selectType;
            var type = ret.value.type;
            var row = ret.value.row;
            if (selectType == 10) {
                if (row) {
                    vm.searchData.S_ClientName = row.name;
                    vm.searchData.S_ClientID = row.id;
                } else {
                    vm.searchData.S_ClientName = '';
                    vm.searchData.S_ClientID = 0;
                }

            }

        });

        api.addEventListener({
            name: 'keyback'
        }, function (ret, err) {
            vm.onClickLeft();
        });

        api.addEventListener({
            name: 'refreshDraft'
        }, function (ret, err) {
            vm.getList();
        })

        var userInfo = FLD.getUserInfo();
        var tabActive = api.pageParam.tabActive;
        var dateIndex = 0;
        vm = new Vue({
            el: '#app',
            data: {
                showLoading: false,
                showSearch: false,
                showDelete: false,
                tabActive: tabActive || 0,
                dataList1: [],
                dataList2: [],
                dataList3: [],
                dataList4: [],
                currentDate: new Date(),
                showDate: false,
                searchData: {
                    S_ClientID: 0,
                    S_ClientName: '',
                    S_BranchID: userInfo.S_BranchID,
                    S_SDate: '',
                    S_EDate: ''
                }

            },
            created: function () {
                this.getList();
            },
            methods: {
                getList() {
                    var that = this;
                    that.showLoading = true;
                    var sql1 = "select * from Draft where Type=101 order by CreateTime desc";
                    DB.selectSql(sql1, function (data) {
                        that.showLoading = false;
                        for (var i in data) {
                            var row = JSON.parse(data[i].JSONStr1);
                            if (row.S_ClientName) {
                                var update_json = {
                                    ClientID: row.S_ClientID,
                                    ClientName: row.S_ClientName,
                                    RecMoney: row.S_RecMoney,
                                    PerMoney: row.S_PerMoney,
                                    BranchID: row.S_BranchID,
                                    BranchName: row.S_BranchName,
                                    EmplID: row.S_EmplID,
                                    EmplName: row.S_EmplName,
                                    ClassID: row.S_ClassID,
                                    ClassName: row.S_ClassName,
                                    StockID: row.S_StockID,
                                    StockName: row.S_StockName,
                                    CargoID: row.S_CargoID,
                                    CargoName: row.S_CargoName,
                                    ProjectID: row.S_ProjectID,
                                    ProjectName: row.S_ProjectName,
                                    VIPCardID: 0,
                                    VIPName: '',
                                    PriceN: row.S_PriceN,
                                    PriceNName: row.S_PriceNName,
                                    Explain: row.Explain,
                                    BType: row.BType,
                                    BTypeName: row.BTypeName,
                                }
                                data[i].JSONStr1 = JSON.stringify(update_json);
                            }
                        }
                        that.dataList1 = data;
                    })
                    var sql2 = "select * from Draft where Type=401 order by CreateTime desc";
                    DB.selectSql(sql2, function (data) {
                        that.showLoading = false;
                        for (var i in data) {
                            var row = JSON.parse(data[i].JSONStr1);
                            if (row.S_ClientName) {
                                var update_json = {
                                    ClientID: row.S_ClientID,
                                    ClientName: row.S_ClientName,
                                    AXMoney: row.S_AXMoney,
                                    YXMoney: row.S_YXMoney,
                                    PerMoney: row.S_PerMoney,
                                    BranchID: row.S_BranchID,
                                    BranchName: row.S_BranchName,
                                    EmplID: row.S_EmplID,
                                    EmplName: row.S_EmplName,
                                    ClassID: row.S_ClassID,
                                    ClassName: row.S_ClassName,
                                    Explain: row.Explain
                                }
                                data[i].JSONStr1 = JSON.stringify(update_json);
                            }
                        }
                        that.dataList2 = data;
                    })
                    var sql3 = "select * from Draft where Type=92 order by CreateTime desc";
                    DB.selectSql(sql3, function (data) {
                        that.showLoading = false;
                        for (var i in data) {
                            var row = JSON.parse(data[i].JSONStr1);
                            if (row.S_ClientName) {
                                var update_json = {
                                    N_ClientID: row.N_ClientID,
                                    N_ClientName: row.N_ClientName,
                                    ClientID: row.S_ClientID,
                                    ClientName: row.S_ClientName,
                                }
                                data[i].JSONStr1 = JSON.stringify(update_json);
                            }
                        }
                        that.dataList3 = data;
                    })
                    var sql4 = "select * from Draft where Type=93 order by CreateTime desc";
                    DB.selectSql(sql4, function (data) {
                        that.showLoading = false;
                        that.dataList4 = data;
                    })
                },
                deleteRow(i, row, index) {
                    var that = this;
                    this.$dialog.confirm({
                        title: '提示',
                        message: '是否删除该条草稿?'
                    }).then(() => {
                        var sql = "delete from Draft where DraftID='" + row.DraftID + "'";
                        DB.executeSql(sql, function () {
                            layer.open({
                                content: '删除成功',
                                skin: 'msg',
                                time: 2
                            });
                            that['dataList' + i].splice(index, 1);
                        })

                    }).catch(() => {

                    });

                },
                getClientName(row) {
                    var new_json = JSON.parse(row.JSONStr1);
                    return new_json.ClientName || '暂无客户信息';
                },
                getTotalNum(row) {
                    var array = JSON.parse(row.JSONStr2);
                    var totalNum = 0;
                    for (var i in array) {
                        totalNum += array[i].num;
                    }
                    return totalNum;
                },
                getTotalPrice1(row) {
                    var array = JSON.parse(row.JSONStr2);
                    var totalPrice = 0;
                    for (var i in array) {
                        totalPrice += array[i].num * parseFloat(array[i].price)
                    }
                    return totalPrice.toFixed(2);
                },
                getTotalPrice2(row) {
                    var array = JSON.parse(row.JSONStr2);
                    var totalPrice = 0;
                    for (var i in array) {
                        totalPrice += parseFloat(array[i].price)
                    }
                    return totalPrice.toFixed(2);
                },
                getTotalPrice3(row) {
                    var array = JSON.parse(row.JSONStr2);
                    var totalPrice = 0;
                    for (var i in array) {
                        totalPrice += parseFloat(array[i].sellPrice);
                    }
                    return totalPrice.toFixed(2);
                },
                getActiveName(row) {
                    var new_json = JSON.parse(row.JSONStr1);
                    return new_json.ProjectName;
                },
                getRechargePrice(row) {
                    var new_json = JSON.parse(row.JSONStr1);
                    return new_json.RecMoney;
                },
                searchList() {
                    var that = this;
                    that.showSearch = false;

                    that.pageIndex1 = 1;
                    that.pageIndex2 = 1;
                    that.pageIndex3 = 1;

                    this.getList();
                },
                openScreen(type, index) {
                    var obj = {
                        S_Type: index,
                        S_Cont: '',
                        S_BranchID: userInfo.S_BranchID,
                        S_SXZ: '',
                        S_StockID: userInfo.S_StockID,
                        S_CargoID: userInfo.S_CargoID,
                        S_BarCode: '',
                        S_PriceN: 0,
                        S_ProductID: 0
                    }

                    api.openWin({
                        name: '筛选' + index,
                        url: 'baseScreen.html',
                        reload: true,
                        slidBackEnabled: false,
                        pageParam: {
                            filterData: obj,
                            type: type,
                            allowClear: true
                        }
                    })

                },
                openDate(index) {
                    dateIndex = index;
                    this.currentDate = new Date()
                    this.showDate = true;
                },
                confirmDate(value) {
                    if (dateIndex == 1) {
                        this.searchData.S_SDate = FLD.getFormatDate(value).split(' ')[0]
                    } else {
                        this.searchData.S_EDate = FLD.getFormatDate(value).split(' ')[0]
                    }
                    this.showDate = false;
                },
                cancelDate(value) {
                    if (dateIndex == 1) {
                        this.searchData.S_SDate = ''
                    } else {
                        this.searchData.S_EDate = '';
                    }
                    this.showDate = false;
                },
                intoRow(row) {
                    if (row.Type == 101) {
                        api.openWin({
                            name: 'selling',
                            url: 'selling.html',
                            reload: true,
                            slidBackEnabled: false,
                            pageParam: {
                                draftID: row.DraftID
                            }
                        })
                    } else if (row.Type == 401) {
                        api.openWin({
                            name: 'receipt',
                            url: 'receipt.html',
                            reload: true,
                            slidBackEnabled: false,
                            pageParam: {
                                draftID: row.DraftID
                            }
                        })
                    } else if (row.Type == 92) {
                        api.openWin({
                            name: 'seasoningCondiments',
                            url: 'seasoningCondiments.html',
                            reload: true,
                            slidBackEnabled: false,
                            pageParam: {
                                draftID: row.DraftID
                            }
                        })
                    } else if (row.Type == 93) {
                        api.openWin({
                            name: 'activityRecharge',
                            url: 'activityRecharge.html',
                            reload: true,
                            slidBackEnabled: false,
                            pageParam: {
                                draftID: row.DraftID
                            }
                        })
                    }

                },
                onClickLeft() {
                    api.sendEvent({
                        name: 'setOrientation'
                    });
                    api.closeWin();
                }
            }
        })
    };
</script>

</html>